package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 大攻略controller
 */
@Controller
@RequestMapping("/strategy")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IRegionService regionService;

    /**
     * 分页及高查
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") StrategyQueryObject qo, Model model){
        model.addAttribute("pageInfo",strategyService.query(qo));
        model.addAttribute("regions",regionService.listAll(null));
        return "strategy/list";
    }

    /**
     * 新增与保存
     * @param strategy
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Strategy strategy) {
        JsonResult jsonResult = new JsonResult();

        try {
            if (strategy.getId() == null) {
                strategyService.save(strategy);
            }else{
                strategyService.update(strategy);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("操作异常");
        }
        return jsonResult;
    }


}
